<?php defined('SYSPATH') or die('No direct script access.');

class Model_Payment extends ORM {

	public function exist($id)
	{
		$data = $this->where('txn_id', '=', $id)->find();
		if($data->id !== NULL)
		{
			return TRUE;
		}
		
		return FALSE;
	}
	
	public function get($user)
	{
		// Date Range
		$from = date('Y-m-d').' 00:00:00';
		$to = date('Y-m-d').' 23:59:59';
		
		if(isset($_GET['from']) AND isset($_GET['to']))
		{
			$get = Validate::factory($_GET)
					->rule('from', 'not_empty')
					->rule('to', 'not_empty')
					->rule('from', 'date')
					->rule('to', 'date');
		
			if ($get->check())
			{
				$from = date('Y-m-d H:i:s',strtotime($_GET['from']));
				$to = date('Y-m-d H:i:s',strtotime($_GET['to']));
			}
			else
			{
				#Affects errors for further display
				Session::instance()->set('errors', $get->errors('daterange'));
			}
		}
		
		$this->where( 'date', 'BETWEEN', DB::expr("'".$from."' AND '".$to."'"));
		
		// Access ID
		if(is_array($user->access_id) AND count($user->access_id) > 0)
		{
			$this->where_open();
			foreach($user->access_id as $id)
			{
				 $this->or_where('id_oper', '=', $id);
			}
			$this->where_close();
		}
		elseif(is_string($user->access_id) AND $user->access_id !== NULL)
		{
			$this->where('id_oper', '=', $user->access_id);
		}

		// Order by
		$this->order_by('date', 'DESC');
		
		return $this;
	}

}